﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace RAgency.User
{
    public partial class UJobs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            DateTime sd = new DateTime(1999, 11, 11, 00, 00, 00, 00);
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            SqlCommand command = new SqlCommand("select access from users where (login='" + User.Identity.Name + "')", sqlc);
            sqlc.Open();
            SqlDataReader sqldr = command.ExecuteReader();
            if (sqldr.HasRows)
            {
                while (sqldr.Read())
                {
                    DateLabel.Text = sqldr.GetDateTime(0).ToString();
                    dt = sqldr.GetDateTime(0);
                }
            }
            else
            {
                DateLabel.Text = "No rows found.";
            }
            sqldr.Close();

            if (dt.Date >= DateTime.Now)
            {
                DateLabel.Text = "Доступ на просмотр БД анкет открыт.";
            }
            else if (dt.Date == sd.Date)
            {
                DateLabel.Text = "Одноразовый доступ открыт. При следующей загрузке анкет доступ будет закрыт.";
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
                try
                {
                    SqlCommand comm = new SqlCommand("UPDATE users SET access='01.01.2000' WHERE login='" + User.Identity.Name + "'", conn);
                    conn.Open();
                    comm.ExecuteReader();
                }
                catch (Exception ex)
                {
                    DateLabel.Text = ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                Response.Redirect("~/About.aspx");
            }
        }
    }
}